package com.example.esc_im_client.common.base;


/**
 * @Author: zhaoyang
 * @Data: 2021/10/22 15：01
 * @Description
 */
public class BaseResponseDTO<T> {

    /**
     * 返回状态码
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String msg;

    /**
     * 相应参数
     */
    private T data;

    private BaseResponseDTO() {
    }

    public BaseResponseDTO(HttpStatusCodeEM httpStatusCodeEM, T data) {
        this.code = httpStatusCodeEM.getCode();
        this.msg = httpStatusCodeEM.getMsg();
        this.data = data;
    }
    public BaseResponseDTO(HttpStatusCodeEM httpStatusCodeEM, String msg, T data) {
        this.code = httpStatusCodeEM.getCode();
        this.msg = msg;
        this.data = data;
    }

    public Integer getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public Object getData() {
        return this.data;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setDate(final T data) {
        this.data = data;
    }


    @Override
    public String toString() {
        return "BaseResponse(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    /**
     * http请求返回的方法，以下都是
     */
    public static BaseResponseDTO<Object> setResultError(String message) {
        return setResult(HttpStatusCodeEM.RESP_FAILED, message, null);
    }

    public static BaseResponseDTO<Object> setResultError(HttpStatusCodeEM httpStatusCodeEM, String message) {
        return setResult(httpStatusCodeEM, message, null);
    }

    public static BaseResponseDTO<Object> setResultError() {
        return setResult(HttpStatusCodeEM.RESP_FAILED, "error", null);
    }

    public static BaseResponseDTO<Object> setResultError(Object data) {
        return setResult(HttpStatusCodeEM.RESP_FAILED, "error", data);
    }

    public static BaseResponseDTO<Object> setResultSuccess(String message) {
        return setResult(HttpStatusCodeEM.RESP_SUCCEED, message, null);
    }
    public static BaseResponseDTO<Object> setResultSuccess() {
        return setResult(HttpStatusCodeEM.RESP_SUCCEED, "success", null);
    }

    public static BaseResponseDTO<Object> setResultSuccess(Object data) {
        return setResult(HttpStatusCodeEM.RESP_SUCCEED, "success", data);
    }


    public static BaseResponseDTO<Object> setResult(HttpStatusCodeEM httpStatusCodeEM, String message, Object data) {
        return new BaseResponseDTO<>(httpStatusCodeEM, message, data);
    }

    /**
     * 直接传入枚举，自动解析 code和 msg
     * @param httpStatusCodeEM 枚举
     * @return 响应结构体
     */
    public static BaseResponseDTO<Object> setResult(HttpStatusCodeEM httpStatusCodeEM) {
        return new BaseResponseDTO<>(httpStatusCodeEM, null);
    }
}
